package com.ruoyi.bearing.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.ruoyi.bearing.domain.BearingModel;
import com.ruoyi.bearing.service.IBearingFeatureService;
import com.ruoyi.common.constant.FlaskDataConstants;
import com.ruoyi.common.constant.FlaskModelConstants;
import com.ruoyi.common.enums.FilteringType;
import com.ruoyi.common.exception.flask.FlaskException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.flask.HttpFlask;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 信号特征分析
 */
@Service
public class BearingFeatureServiceImpl implements IBearingFeatureService {
    /**
     * 信号特征提取
     */
    public String startBearingFeatureExtraction(MultipartFile file, BearingModel bearingModel) throws IOException, IllegalAccessException {
        // 请求动态路径
        String res = HttpFlask.sendGetFlask(FlaskModelConstants.Model_CSV_URL);
        if (!"".equals(res)) {
            JSONObject jsonObject = JSONObject.parseObject(res);
            String dir = (String) jsonObject.get("data");
            String csvName = FileUploadUtils.upload(dir, file);
            String csvFileName = csvName.split("csv/")[1];
            bearingModel.setFileName(csvFileName);
            String postParam = HttpFlask.getKeyAndValueString(bearingModel);
            // 进行特征提取
            String featureExtractionRes = HttpFlask.sendPostFlask(FlaskDataConstants.Data_FEATURE_EXTRACTION_URL, postParam);
            if (!"".equals(featureExtractionRes)) {
                JSONObject featureExtraction = JSONObject.parseObject(featureExtractionRes);
                return (String) featureExtraction.get("data");
            }
        }
        return null;
    }

    /**
     * 特征选择
     */
    public String startBearingFeatureSelection(MultipartFile file, BearingModel bearingModel) throws IOException, IllegalAccessException {
        // 请求动态路径
        String res = HttpFlask.sendGetFlask(FlaskModelConstants.Model_CSV_URL);
        if (!"".equals(res)) {
            JSONObject jsonObject = JSONObject.parseObject(res);
            String dir = (String) jsonObject.get("data");
            String csvName = FileUploadUtils.upload(dir, file);
            String csvFileName = csvName.split("csv/")[1];
            bearingModel.setFileName(csvFileName);
            String postParam = HttpFlask.getKeyAndValueString(bearingModel);
            // 进行特征提取
            String featureSelectionRes = HttpFlask.sendPostFlask(FlaskDataConstants.Data_FEATURE_SELECTION_URL, postParam);
            if (!"".equals(featureSelectionRes)) {
                JSONObject featureSelection = JSONObject.parseObject(featureSelectionRes);
                return (String) featureSelection.get("data");
            }
        }
        return null;
    }

    /**
     * 将csv文件发送到前端
     */
    public void csvToVue(String filePath, OutputStream outputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String line;
        while ((line = reader.readLine()) != null) {
            outputStream.write(line.getBytes(StandardCharsets.UTF_8));
            outputStream.write('\n');
        }
        reader.close();
        outputStream.close();
    }

}
